/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.pg.misra.rules;

import java.util.ArrayList;
import java.util.List;
import pl.pg.misra.AST.AST;
import pl.pg.misra.Interfaces.IRule;

/**
 *
 * @author Paweł
 */
public class SingleReturnRule extends AbstractRule implements IRule {

    @Override
    public List<String> checkRule(List<AST> asts) {
        List<String> result = new ArrayList<>();
        List<AST> searchResult;
        
        for (AST function : asts){
            searchResult = new ArrayList<>();
            
            if (!function.getAttributes().get(0).toLowerCase().contains("void")){
                searchResult = findNodesByType("ReturnStmt", function, searchResult);
                
                if (searchResult.isEmpty()){
                    result.add(buildViolationMessage(function, "14.7", "no return statement"));
                }
                else if (searchResult.size() == 1){
                    if (!function.getChildrens().get(function.getChildrens().size() - 1).getNodeType().equals("ReturnStmt")){
                        result.add(buildViolationMessage(function, "14.7", "no return statement at the end of function"));
                    }
                }
                else {
                    result.add(buildViolationMessage(function, "14.7", "multiple return statements"));
                }    
            }
        }
        
        return result;
    }
    
}
